// pages/ucenter/my-funds/my-funds.js
const app = getApp();
import * as API_Members from '../../api/members'
import * as API_Distribution from '../../api/distribution'
import { Foundation } from '../../ui-utils/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop_id:null,
    showType: 0,
    fundList:[],
    params: {
      page_no: 1,
      page_size: 20,
      is_withdraw:1
    },
    finished: false,
    showGoTop: false,
    scrollTop: 0,//滚动高度
    scrollHeight: '',
    pageCount: 0,
    //待分润
    daifenrun:'',
    //已分润
    yifenrun:'',
    //基金总金额
    allFuns:'',
    msg:''
  },
  switchTab(event) {
    let index = parseInt(event.currentTarget.dataset.index)
    console.log(index)
    
    if (index === 0){
      this.setData({
        'params.page_no': 1,
        fundList: [],
        finished: false,
        'params.is_withdraw':1,
        shop_id:null
      })
    }
    if (index === 1){
      this.setData({
        'params.page_no': 1,
        fundList: [],
        finished: false,
        'params.is_withdraw':0
      })
    }
    this.getFundsList();
    this.setData({showType: index,})
    
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({shop_id:options.shop_id})
    API_Distribution.getFenRun(options.shop_id).then(response => {
      this.setData({
        daifenrun : Number(response.daifenrun).toFixed(2),
        yifenrun : Number(response.yifenrun).toFixed(2),
        allFuns : (
          Number(response.daifenrun) + Number(response.yifenrun)
        ).toFixed(2)
      })
    });
    if(options.shop_id){
      this.setData({
        params:{
          page_no: 1,
          page_size: 20,
          is_withdraw:1,
          shop_id:options.shop_id
        }
      })
      this.getFundsList();
    }else{
      this.getFundsList()
    }
    this.setData({
      scrollHeight: wx.getSystemInfoSync().windowHeight - 118
    })
    
  },
  getFundsList(){
    let ispublic = this.data.shop_id ? false : true
    API_Distribution.getFenRundetail(ispublic,this.data.params).then(response=>{
      let _pageCount = Math.ceil(response.data_total / this.data.params.page_size)
      this.setData({ pageCount: _pageCount })
      let _funds = response.data
      if(_funds && _funds.length){
        _funds.forEach(key => {
          key.create_time = Foundation.unixToDate(key.create_time, 'yyyy-MM-dd')
        })
        this.setData({ fundList: this.data.fundList.concat(_funds)})
      }else{
        this.setData({ finished: true })
      } 
    })
  },
  loadMore: function () {
    if (!this.data.finshed) {
      this.setData({ "params.page_no": this.data.params.page_no + 1 })
      console.log(this.data.pageCount)
      if (this.data.pageCount >= this.data.params.page_no) {
        this.getFundsList()
      } else {
        this.setData({ msg: '已经到底了~' })
      }
    }
  },
  scroll: function (e) {
    let that = this
    if (e.detail.scrollTop > 200) {
      that.setData({showGoTop: true})
    } else {
      that.setData({showGoTop: false})
    }
  },
  //返回顶部
  goTop: function () { this.setData({ scrollTop: 0 })},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})